WooCommerce Stock Management can be easy when you have digital products or products that are by your side, which you store. But what if the products are stored somewhere else, at a wholesaler or some other service? You will need to get the stock level from the service. In this tutorial, we will look into how this can be done.
This tutorial will just show a way on how you can do that but since there are many services and they are all different, I won’t be getting into how to create a complete solution for it but rather show you a guideline on how you can do that.
At the bottom of this tutorial you will also see two videos that I have recorded for this tutorial.
Filtering the Stock Level
WooCommerce is following the WordPress Standards and thus it uses also the WordPress Plugin API so that you can easily extend or change various parts of WooCommerce.
They have thought about the stock management also!
You can view this method inside the class WC_Product
. With the WordPress Plugin API, we can hook into that filter with a function to return the stock level from our service.
This is an example on how you can hook a function into that. Within that function you can easily return the stock level. This level can be returned from a service.
Product Fields
When you are working with a service, your products will have a unique ID from that service. This ID should be saved somewhere so that we can synchronise the stock level with the appropriate product.
WooCommerce provides hooks to create the fields on various sections. For making the WooCommerce Stock Management easier, we want to hook our field into the inventory section. Here is the hook which we can use:
Inside that hook, we have a function that should create a WooCommerce field. For various field functions, you can view the file at woocommerce/includes/admin/wc-meta-box-functions.php
.
For the ID we can use the function woocommerce_wp_text_input( $args )
.
Saving the Product field
You also need a way to save the ID of our Product. We can do that by hooking into:
Video Tutorial
I have also create two short videos for this also which you can view on my channel or here:
Introduction
Filtering the Stock Level
Learn more about WooCommerce Stock Management
The code that you have seen here and much more (functions, api connection example) can be read in my eBook:
This eBook will help you create your own Shipping method and your own Payment gateway along other smaller but also important parts of WooCommerce.
Become a Sponsor
Hi Benic,
I would like to ask you something. See my scenario below.
I have a Website with woo-commerce running. The Products are in the warehouse and they wanted the quantity(stock) to be synced with the website woo-commerce platform.
They have given us web-service, xml format to pull the data from the warehouse. I am able to pull the data for a particular product. Initially they aren’t adding any stock levels while creating the product. All the stock availability is pulled from the web service.
This web-service is been called in single-product template. When ever user opens any product, based on its ID, it pulls data and displays the available stock quantity from warehouse. Now what i need is, can we insert this web-service data to product stock quantity so that it can be synced. Can you please guide me on how to achieve this.
You can add that data by attaching a function to it using the filter
woocommerce_get_stock_quantity
. A way you could approach it is to check if you are on the product template. This can be done using the filtertemplate_include
. There you can do the logic for finding out if that page is a WooCommerce product. After that, request the web service and get the stock level. Save the stock level in a static variable or a global.I would have a singleton object to get all of that there.
Once that is done, hook a function to the filter
woocommerce_get_stock_quantity
and return the value from the service.Or, if you are just getting the services just to check the stock level, then you can do that all inside the filter
woocommerce_get_stock_quantity
.